clear all
set more off
cap log close

****************************************************************************
* This do-file extract data from the Read-only LAD files
*
* 
****************************************************************************

do "H:\Lavecchia_7086\to-transfer-jan-2022\RESTAT_Replication_Programs\0_Set_Directories.do"

cap log using "$dir_log\1h_Extract_SpousalCG.log", replace


* Step 1: load the data from LAD read-only files


local year=1982
use lin__i clkgxp`year' txi__p`year' tirc_p`year'  using "G:\LAD\LAD_DAL_Allcycle_v3\data\data\Stata\lad`year'.dta"
sort lin__i
gen year=`year'
rename clkgxp`year' clkgxp
rename txi__p`year'   txi__p
rename tirc_p`year'   tirc_p
save "$dir_data\Capital income years\capital_income_spousal_`year'.dta", replace


forvalues year=1983/1985{
clear
use klpycp`year' lin__i clkgxp`year' txi__p`year' tirc_p`year'  using "G:\LAD\LAD_DAL_Allcycle_v3\data\data\Stata\lad`year'.dta"
sort lin__i
gen year=`year'
rename klpycp`year'  klpycp
rename clkgxp`year' clkgxp
rename txi__p`year'   txi__p
rename tirc_p`year'   tirc_p
save "$dir_data\Capital income years\capital_income_spousal_`year'.dta", replace
}

forvalues year=1986/2016{
clear
use ggex_p`year' klpycp`year'  lin__i clkgxp`year' txi__p`year' tirc_p`year'  using "G:\LAD\LAD_DAL_Allcycle_v3\data\data\Stata\lad`year'.dta"
sort lin__i
gen year=`year'
rename ggex_p`year' ggex_p
rename klpycp`year'  klpycp
rename clkgxp`year' clkgxp
rename txi__p`year'   txi__p
rename tirc_p`year'   tirc_p
save "$dir_data\Capital income years\capital_income_spousal_`year'.dta", replace
}



* Step 2: append into one file (rename the variables to drop the year portion of the name)

**************** 1982 -- 1999 ****************
clear
use "$dir_data\Capital income years\capital_income_spousal_1982.dta"
forvalues year=1983/1999{
append using "$dir_data\Capital income years\capital_income_spousal_`year'.dta"
compress
}
*
save "$dir_data\capital_income_spousal_8299.dta", replace


**************** 2000 -- 2016 ****************
clear
use "$dir_data\Capital income years\capital_income_spousal_2000.dta"
forvalues year=2001/2016{
append using "$dir_data\Capital income years\capital_income_spousal_`year'.dta"
compress
}
*
save "$dir_data\capital_income_spousal_0016.dta", replace

*use "H:\Tazhitdinova_5768\2_Alisa_Data\death_8299.dta"
*append using "H:\Tazhitdinova_5768\2_Alisa_Data\death_0016.dta"
*save "H:\Tazhitdinova_5768\2_Alisa_Data\death.dta", replace
